package com.example.yjhmatch.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.example.yjhmatch.entity.MatchsDTO;
import com.yc.yjh.entity.Matches;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 赛程表 Mapper 接口
 * </p>
 *
 * @author 廖老师
 * @since 2023-07-20
 */
public interface MatchesMapper extends BaseMapper<Matches> {
    @Select("select m.*,t1.icon as \"hteams.icon\",t2.icon as \"ateams.icon\"" +
            ",t1.team_id as \"hteams.team_id\",t2.team_id as \"ateams.team_id\" " +
            " FROM matches m" +
            " INNER JOIN teams t1 ON m.home_team_name=t1.name" +
            " INNER JOIN teams t2 ON m.away_team_name=t2.name")
    List<Matches> selectMatchesWithTeams();

    @Select("select m.*,t1.icon as \"hteams.icon\",t2.icon as \"ateams.icon\"," +
            "t1.team_id as \"hteams.team_id\",t2.team_id as \"ateams.team_id\" " +
            " FROM matches m" +
            " INNER JOIN teams t1 ON m.home_team_name=t1.name" +
            " INNER JOIN teams t2 ON m.away_team_name=t2.name where m.date=#{date}")
    List<Matches> selectMatchesByDate(@Param("date") String date);

    @Select("select m.*,t1.icon as \"hteams.icon\",t2.icon as \"ateams.icon\", " +
            "t1.team_id as \"hteams.team_id\",t2.team_id as \"ateams.team_id\"" +
            " FROM matches m" +
            " INNER JOIN teams t1 ON m.home_team_name=t1.name" +
            " INNER JOIN teams t2 ON m.away_team_name=t2.name where m.match_id=${mid}")
    List<Matches> selectMatchById(@Param("mid") String mid);

    @Select("SELECT m.*, t1.icon AS hteams_icon, t2.icon AS ateams_icon\n" +
            "FROM matches m\n" +
            "INNER JOIN teams t1 ON m.home_team_name = t1.name\n" +
            "INNER JOIN teams t2 ON m.away_team_name = t2.name ${ew.customSqlSegment} order by date")
    List<Matches> selectMatchsByWrapper(@Param(Constants.WRAPPER)QueryWrapper wrapper);

    @Select("SELECT * FROM matches WHERE home_team_name = #{teamName} OR away_team_name = #{teamName}")
    List<Matches> findByTeamName(@Param("teamName") String teamName);

    List<MatchsDTO> queryAllMatchsDataByMid();

}
